package com.pureut.storage.controller;


import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.StockOutInfoDto;
import com.pureut.storage.export.ProductionPickingExport;
import com.pureut.storage.service.ProductionPickingService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产领料控制层
 *
 * @author: CM
 * @date: 2022/12/22  15:59
 */

@RestController
@RequestMapping("/productionPicking")
public class ProductionPickingController extends BaseController {

    @Resource
    ProductionPickingService productionPickingService;

    /**
     * 新增生产领料单
     */
    @RequiresPermissions("warehouse:outbound:producfield:add")
    @Log(title = "生产领料", businessType = BusinessType.INSERT)
    @PostMapping("/addPincking")
    public AjaxResult add(@RequestBody ProductionPickingVo productionPickingVo) throws Exception {
        return toAjax(productionPickingService.addProductionPicking(productionPickingVo));
    }

    /**
     * 查询生产领料列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(ProductionPickingVo productionPickingVo) {
        startPage();
        //查询拣货方案列表
        List<StockOutInfoDto> list = productionPickingService.getList(productionPickingVo);
        return getDataTable(list);
    }

    /**
     * 生产确认
     */
    @RequiresPermissions("warehouse:outbound:producfield:confirm")
    @Log(title = "生产领料", businessType = BusinessType.UPDATE)
    @GetMapping("/confirm/{id}")
    public AjaxResult confirm(@PathVariable("id") Long id) throws Exception {
        return toAjax(productionPickingService.confirm(id));
    }

    /**
     * 修改单据
     */
    @RequiresPermissions("warehouse:outbound:producfield:edit")
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDoc")
    public AjaxResult updateDoc(@RequestBody ProductionPickingVo productionPickingVo) throws Exception {
        return toAjax(productionPickingService.updateDoc(productionPickingVo));
    }

    /**
     * 查看单据
     */
    @RequiresPermissions("warehouse:outbound:producfield:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(productionPickingService.view(id));
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:outbound:producfield:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteDoc/{ids}")
    public AjaxResult deleteDoc(@PathVariable("ids") String ids) throws Exception {
        return toAjax(productionPickingService.deleteDoc(ids));
    }

    /**
     * 关结单据
     */
    @RequiresPermissions("warehouse:outbound:otherout:close")
    @Log(title = "关结单据", businessType = BusinessType.UPDATE)
    @PostMapping("/closeDoc")
    public AjaxResult closeDoc(@RequestBody ProductionPickingVo productionPickingVo) throws Exception {
        return toAjax(productionPickingService.closeDoc(productionPickingVo));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:outbound:otherout:export")
    @PostMapping("/export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response, ProductionPickingVo productionPickingVo) throws IOException {
        List<ProductionPickingExport> infoExport = productionPickingService.getInfoExport(productionPickingVo);
        ExcelUtil<ProductionPickingExport> excelUtil = new ExcelUtil<ProductionPickingExport>(ProductionPickingExport.class);
        excelUtil.excelWriter(response, infoExport, "生产领料信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 生产领料出库
     */
    @RequiresPermissions("warehouse:outbound:otherout:outbound")
    @Log(title = "生产领料出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public AjaxResult out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(productionPickingService.out(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    @Log(title = "生产领料-拣货完成", businessType = BusinessType.UPDATE)
    public AjaxResult finishProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(productionPickingService.finishProduction(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealProduction/{id}")
    @Log(title = "生产领料-拣货撤销", businessType = BusinessType.UPDATE)
    public AjaxResult repealProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(productionPickingService.repealProduction(id));
    }

    /**
     * 出库交接
     */
    @RequiresPermissions("warehouse:outbound:otherout:handover")
    @Log(title = "生产领料-出库交接", businessType = BusinessType.UPDATE)
    @PostMapping("/overProduction")
    public AjaxResult overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(productionPickingService.overProduction(pickingRestDbVo));
    }

    /**
     * 生产领料Feign(自动新增)
     */
    @Log(title = "生产领料", businessType = BusinessType.INSERT)
    @PostMapping("/addAutoProduction")
    public AjaxResult add(@RequestBody FeginProductionPickingVo feginProductionPickingVo) throws Exception {
        return toAjax(productionPickingService.addAutoProduction(feginProductionPickingVo));
    }

    /**
     * 脱脂模块生成生产领料单(fegin调用)
     */
    @Log(title = "生产领料", businessType = BusinessType.INSERT)
    @PostMapping("/addAutoProductionByDegreas")
    public AjaxResult addAutoProductionByDegreas(@RequestBody FeginProductionPickingVo feginProductionPickingVo) throws Exception {
        return toAjax(productionPickingService.addAutoProductionByDegreas(feginProductionPickingVo));
    }

    /**
     * 查看当前制令单对应的领料明细是否为空
     */
    @PostMapping("/getMoNumber")
    public AjaxResult getMoNumber(@RequestBody FeedVo feedVo) {
        return toAjax(productionPickingService.getMoNumber(feedVo));
    }

    /**
     * 拣货提示
     */
    @GetMapping("/point/{id}")
    public AjaxResult point(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(productionPickingService.point(id));
    }

    /**
     * 全局拣货提示
     */
    @GetMapping("/getSchemePdaInfo")
    public AjaxResult getSchemePdaInfo(SchemePdaVo schemePdaVo) throws Exception {
        return AjaxResult.success(productionPickingService.getSchemePdaInfo(schemePdaVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(productionPickingService.updateStatus(updateDocVo));
    }

    /**
     * PDA列表
     */
    @GetMapping("/getPdaList")
    public TableDataInfo getPdaList(String number) {
        startPage();
        return getDataTable(productionPickingService.getPdaList(number));
    }

    /**
     * PDA查看详情
     */
    @GetMapping("/getPdaDetail")
    public AjaxResult getPdaDetail(GetPdaVo getPdaVo) {
        return AjaxResult.success(productionPickingService.getPdaDetail(getPdaVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/getPdaFinish")
    public AjaxResult getPdaFinish(GetPdaVo getPdaVo) throws Exception {
        return AjaxResult.success(productionPickingService.getPdaFinish(getPdaVo));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/getCeX")
    public AjaxResult getCeX(GetPdaVo getPdaVo) {
        return AjaxResult.success(productionPickingService.getCeX(getPdaVo));
    }

    /**
     * 脱脂制令单修改时修改相关生产制令单(fegin调用)
     */
    @PostMapping("/updateByDegreas")
    public AjaxResult updateByDegreas(@RequestBody DegreasByUpdateVo degreasByUpdateVo) throws Exception {
        return toAjax(productionPickingService.updateByDegreas(degreasByUpdateVo));
    }

    /**
     * 脱脂制令单删除相关生产制令单(fegin调用)
     */
    @GetMapping("/deleteByDegreas")
    public void deleteByDegreas(String degreasOrderNumber) {
        productionPickingService.deleteByDegreas(degreasOrderNumber);
    }

    /**
     * 水刺制令单号查询数据（fegin）
     */
    @GetMapping("/spunlaceNumber")
    public AjaxResult spunlaceNumber(String spunlaceOrderNumber) {
        return AjaxResult.success(productionPickingService.spunlaceNumber(spunlaceOrderNumber));
    }

    /**
     * 开灯关灯
     */
    @GetMapping("/openLight")
    public AjaxResult openLight(@RequestParam Boolean isOpen) throws Exception {
        Map<String, Object> param = new HashMap<>();
        if (isOpen) {
            param.put("id", "LS1");
            param.put("mode", "flicker");
            param.put("color", "green");
            param.put("spk", "5");
        } else {
            param.put("id", "LS1");
            param.put("mode", "off");
            param.put("color", "yellow");
        }
        HttpUtil.post("http://whywsz.com:18800/openLight", param);
        return toAjax(true);
    }

    /**
     * 前往下一个库位
     */
    @GetMapping("/goLocationSpk")
    public AjaxResult goLocationSpk() throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("id", "LS1");
        param.put("mode", "off");
        param.put("color", "green");
        param.put("spk", "1");
        HttpUtil.post("http://whywsz.com:18800/openLight", param);
        return toAjax(true);
    }
}
